<?php
switch($action) {
case listAct;
	listACt();
	break;
case addNew;
	addNew();
	break;
case editNew;
	editNew();
	break;
case deleteNew;
	deleteNew();
	break;
case listCatalog;
	listCatalog();
	break;
case add;
	add();
	break;
case edit;
	edit();
	break;
case delete;
	delete();
	break;
case orders;
	orders();
	break;
case catalogTree;
	catalogTree();
	break;
}


function deleteNew() {
	global $userInfo,$config;
	$id = intval(_g('id'));
	$Catalog = new Catalog();
	$Catalog->DelteNew($id);
	$message = array(
		'status' => 'success',
		'msg' => '删除成功',
	);
	echo json_encode($message);
	exit;

}


function editNew(){
	global $config,$userInfo;
	if(_g('submit')) {
		$id = intval(_g('id'));
		$data['title'] = trim(_g('title'));
		valance($data['title'],'请填写标题');
		$data['source'] = trim(_g('source'));
		valance($data['source'],'请填写来源');
		$data['webUrl'] = trim(_g('webUrl'));
		valance($data['webUrl'],'请填写来源URL');
		$data['author'] = trim(_g('author'));
		valance($data['author'],'请填写作者');
		$data['classid'] = trim(_g('classid'));
		valance($data['classid'],'请选择分类');
		$catalog = new Catalog();
		$roleIdTreeArray = $catalog->seachUserEditRole($userInfo['roleid']);
		$userBool = RoleUser($data['classid'],$roleIdTreeArray['role']);
		if(!$userBool){
			$message = array(
				'status' => 'error',
				'msg' => '您没有编辑这个分类权限',
			);
			echo json_encode($message);
			exit;
		}
		$catalog = new Catalog();
		$classArray=$catalog->lookRoleModule($data['classid']);
		if(2==$classArray['ispart']){
			$message = array(
				'status' => 'error',
				'msg' => '不能再栏目下发文章',
			);
			echo json_encode($message);
			exit;
		}
		$data['classname'] = $classArray['text'];
		$data['content'] = trim(_g('content'));
		$data['pubdate'] = time();
		$data['keywords'] = trim(_g('keywords'));
		$data['description'] = trim(_g('description'));
		valance($data['content'],'请填写内容');
		$catalog->saveEditNew($data,$id);
		$url = $config['webUrl'].'article.php?id='.$id;
		openurl($url);
		lanHtml($s);
		$message = array(
			'status' => 'success',
			'msg' => $id,
		);
		echo json_encode($message);
		exit;
	}else{
		$id = intval(_g('id'));
		$Catalog = new Catalog();
		$dataArray = $Catalog->getNew($id);
		include template('manage/editNew');
	}
}


function listAct() {
	global $config,$userInfo;
	$Catalog = new Catalog();
	$page = intval(_g('page'))?intval(_g('page')):1;
	$num = $config['num'];
	$sum = $Catalog->getArctnewCount();
	$newList = $Catalog->getArctneList($page,$num,$findkey='',$order='id',$fields='*');
	if(is_array($newList)){
		foreach	($newList as $key=>&$value){
			$value['classarray'] = $Catalog->lookRoleModule($value['classid']);
		}
	}
	$pagination = new pagination();
	$pagination->set($sum,$num,'index.php');
	include template('manage/listAct');
}
/**
 * 添加新闻
 */
function addNew() {
	global $config,$userInfo;
	if(_g('submit')) {
		$data['title'] = trim(_g('title'));
		valance($data['title'],'请填写标题');
		$data['source'] = trim(_g('source'));
		valance($data['source'],'请填写来源');
		$data['webUrl'] = trim(_g('webUrl'));
		valance($data['webUrl'],'请填写来源URL');
		$data['author'] = trim(_g('author'));
		valance($data['author'],'请填写作者');
		$data['classid'] = trim(_g('classid'));
		valance($data['classid'],'请选择分类');
		$catalog = new Catalog();
		$roleIdTreeArray = $catalog->seachUserEditRole($userInfo['roleid']);
		$userBool = RoleUser($data['classid'],$roleIdTreeArray['role']);
		if(!$userBool){
			$message = array(
				'status' => 'error',
				'msg' => '您没有编辑这个分类权限',
			);
			echo json_encode($message);
			exit;
		}
		$catalog = new Catalog();
		$classArray=$catalog->lookRoleModule($data['classid']);
		if(2==$classArray['ispart']){
			$message = array(
				'status' => 'error',
				'msg' => '不能再栏目下发文章',
			);
			echo json_encode($message);
			exit;
		}
		$data['classname'] = $classArray['text'];
		$data['content'] = trim(_g('content'));
		$data['pubdate'] = time();
		$data['mid'] = rand_string(8);
		$data['keywords'] = trim(_g('keywords'));
		$data['description'] = trim(_g('description'));
		//$data['flag'] = trim(_g('flag'));
		valance($data['content'],'请填写内容');
		$id =$catalog->saveAddNew($data);
		$url = $config['webUrl'].'article.php?id='.$id;
		openurl($url);
		lanHtml($s);
		$message = array(
			'status' => 'success',
			'msg' => $id,
		);
		echo json_encode($message);
		exit;
	}else{
		include template('manage/addNew');
	}
}


















































/*分类列表*/
function listCatalog() {
	global $config,$userInfo;
	$Catalog = new Catalog();
	$module = $Catalog->catalogTreeSort();
	include template('manage/listCatalog');
}

/*获取分类tree*/
function catalogTree() {
	global $config,$userInfo;
	$Catalog = new Catalog();
	$data = $Catalog->catalogTreeSort();
	foreach ($data as $key=>$value){
			$dataShow[] = $value;
	}
	$avail['id'] = '-1';
	$avail['text'] = '无选择项';
	$avail['iconCls'] = 'icon-cancel';
	$dataShow[] = $avail;
	$lan = trim(_g('lan'));
	if(!$lan){
		$dataShow = getEditTree($dataShow,$userInfo['roleid']);
	}else{
		$dataShow = getEditTree($dataShow);
	}
	exit(json_encode($dataShow));
}

/*添加分类*/
function add() {
	global $config,$userInfo;
	if(_g('submit')) {
		$data['text'] = trim(_g('module_name'));
		if(!$data['text']) {
			$message = array(
				'status' => 'error',
				'msg' => '分类名称未填写',
			);
			echo json_encode($message);
			exit;
		}
		$Catalog = new Catalog();
		$data['pid'] = max(0,_g('pid'));
		$pModule = $Catalog->lookRoleModule(_g('pid'));
		$data['mid'] = rand_string();
		$data['ptext'] = $pModule['text'];
		$data['binid'] = max(0,_g('binid'));
		$data['typedir'] = trim(_g('typedir'));
		$data['keyword'] = trim(_g('keyword'));
		$data['description'] = trim(_g('description'));
		$data['tempindex'] = trim(_g('tempindex'));
		$data['templist'] = trim(_g('templist'));
		$data['temparticle'] = trim(_g('temparticle'));;
		$data['ispart'] = intval(_g('ispart'));
		$bModule = $Catalog->lookRoleModule($data['binid']);
		$data['btext'] = $bModule['text'];
		$id = $Catalog->addRoleModule($data);
		if($id) {
			$message = array(
				'status' => 'OK',
				'msg' => '更新成功',
			);
			echo json_encode($message);
			exit;
		}else{
			$message = array(
				'status' => 'error',
				'msg' => $id,
			);
			echo json_encode($message);
			exit;
		}
	}else{
		include template('manage/addCtatlog');
	}
}

/*编辑分类*/
function edit() {
	global $config,$userInfo;
	if(_g('submit')) {
		$id = intval(_g('id'));
		if(!$id) {
			$message = array(
				'status' => 'error',
				'msg' => '请刷新页面数据未知错误',
			);
			echo json_encode($message);
			exit;
		}
		$data['text'] = trim(_g('module_name'));
		if(!$data['text']) {
			$message = array(
				'status' => 'error',
				'msg' => '请刷新页面数据未知错误',
			);
			echo json_encode($message);
			exit;
		}
		$Catalog = new Catalog();
		$data['pid'] = max(0,_g('pid'));
		$pModule = $Catalog->lookRoleModule(_g('pid'));
		$data['ptext'] = $pModule['text'];
		$data['binid'] = max(0,_g('binid'));
		$bModule = $Catalog->lookRoleModule(_g('binid'));
		$data['ptext'] = $pModule['text'];
		$data['binid'] = max(0,_g('binid'));
		$data['typedir'] = trim(_g('typedir'));
		$data['keyword'] = trim(_g('keyword'));
		$data['description'] = trim(_g('description'));
		$data['tempindex'] = trim(_g('tempindex'));
		$data['templist'] = trim(_g('templist'));
		$data['temparticle'] = trim(_g('temparticle'));
		$data['ispart'] = intval(_g('ispart'));
		$Catalog->editSaveModule($data,$id);
		$message = array(
			'status' => 'OK',
			'msg' => '更新成功',
		);
		echo json_encode($message);
		exit;
	}else{
		$id = intval(_g('module_id'));
		$Catalog = new Catalog();
		$module = $Catalog->lookRoleModule($id);
		include template('manage/editCtatlog');
	}
}

/*删除分类*/
function delete() {
	global $config,$userInfo;
	$id = intval(_g('module_id'));
	$Catalog = new Catalog();
	$module = $Catalog->DelteClass($id);
	$message = array(
		'status' => 'success',
		'msg' => '删除成功',
	);
	echo json_encode($message);
	exit;
}

/*分类排序*/
function orders() {
	global $config,$userInfo;
	$id = intval(_g('id'));
	$value = intval(_g('sortrank'));
	$Catalog = new Catalog();
	$Catalog->taxisModulegory($value,$id);
	$message = array(
		'status' => 'success',
		'value' => $value,
	);
	echo json_encode($message);
	exit;
}



























































?>